package productionLine

import (
	"context"
	"github.com/google/uuid"
	"github.com/kuangshp/go-utils/k"
	"insighttracking/query/dao"
	"insighttracking/query/model"
	"net/http"

	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetProductionLineListApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetProductionLineListApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetProductionLineListApiLogic {
	hostname, _ := os.Hostname()
	return &GetProductionLineListApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}
func (l *GetProductionLineListApiLogic) GetProductionLineListApi() (resp []types.ProductionLineListResp, err error) {
	productionLineEntities, err := dao.ProductionLineEntity.WithContext(l.ctx).Where(dao.ProductionLineEntity.Status.Eq(1)).Order(dao.ProductionLineEntity.Sort).Find()
	if err != nil {
		return make([]types.ProductionLineListResp, 0), nil
	}
	resps := k.Map(productionLineEntities, func(item *model.ProductionLineEntity, index int) types.ProductionLineListResp {
		return types.ProductionLineListResp{
			ID:   item.ID,
			Name: item.Name,
		}
	})
	return resps, nil
}
